define(["jquery","bbcare","bch/app/selfRecord/selfRecordComm.servers","bch/app/selfRecord/selfRecordComm.ui","bch/app/selfRecord/selfRecordComm.event",'bch/app/selfRecord/pregnancySelfRecord.servers','bch/app/selfRecord/pregnancySelfRecord.ui'],function($,$bb,$selfRecordCommServers,$selfRecordCommUi,$selfRecordCommEvent,$servers,$ui){
	var pregnancySelfRecord = {};

	/**
	 * 请求备孕妈妈BMI值成功后的回调函数
	 */
	var ajaxPrepregnancyBmiCallBack = function(data,params){
		if( data.weightList.length < 10 ){
			var len = 10 - data.weightList.length;
			for (var i = 0; i < len; i++) {
				data.weightList.push({});
			}
		}
		$ui.bmiTable({
			"id":"bmiTable",
			"list":data.weightList
		});
	}

	/**
	 * 请求胎动记录成功后的回调函数
	 */
	var ajaxFetalmovementCallBack = function(data,params){
		$ui.fmTable({
			"id":"fmTable",
			"list":data.pregnancyWeekList
		});
	}

	/**
	 * 微信用户建档绑定模块 / 根据传入paramCode 查询问卷（新生儿早期记录1-7天问卷查询）
	 */
	var ajaxChildcareConDataCallBack = function(data,datas){
		var datas2 = $selfRecordCommServers.handleTable(datas,data.dayValueList);
		$selfRecordCommUi.table({
			"id":"selfTable",
			"data":datas2
		});
	}

	/**
	 * 自我检查记录 / 查询患者任务问卷及答案请求成功后的回调函数
	 */
	var ajaxSelfRecordTemplCallBack = function(data,taskData){
		var datas = $selfRecordCommServers.handleTable(data);
		$selfRecordCommUi.table({
			"id":"selfTable",
			"data":datas
		});
		if( datas.flag ) $selfRecordCommServers.ajaxChildcareConData(taskData,ajaxChildcareConDataCallBack,data);
	}

	/**
	 * 选择xx自我记录
	 */
	var onTaskSelect = function(){
		$(this).addClass("active").siblings().removeClass("active");
		var index = $(this).index();
		var data = $(this).data("data");
		$selfRecordCommServers.ajaxSelfRecordTempl(data.rows[index],ajaxSelfRecordTemplCallBack);
	}

	/**
	 * 请求自我检查记录 / 查询患者所有任务成功后的回调函数
	 */
	var ajaxUserCpTasksCallBack = function(data,params){
		var id = "tabShow";
		if( data.rows.length > 0 ){
			$(".tab-wrap").css({"display":"block"});
			$selfRecordCommUi.tab({
				"id":id,
				"list":data.rows
			});
			// 初始化
			$selfRecordCommEvent.carouselInit(0);
			$("#" + id + " li").unbind("click");
			$("#" + id + " li").data("data",{"rows":data.rows,"index":params.index});
			$("#" + id + " li").click(onTaskSelect);
			$("#" + id + " li").eq(0).click();
		}else{
			$(".tab-wrap").css({"display":"none"});
			$("#selfTable").html('<div class="selfRecord-none">该孕妇没有做自我记录！</div>');
		}
	}

	/**
	 * 请求听课列表成功后的回调函数
	 */
	var ajaxCourseCallBack = function(data,params){
		if( data.rows.length < params.rows ){
			var len = params.rows - data.rows.length;
			for (var i = 0; i < len; i++) {
				data.rows.push({});
			}
		}
		$ui.courseTable({
			"id":"courseTable",
			"list":data.rows
		});
		if( data.total > 0 ){
			var pagesAll = Math.ceil(data.total / params.rows);
			$bb.page.init({
				"id":"courseTablePage",
				"pagesAll":pagesAll,
				"page":params.page,
				"params":params,
				"callback":courseInit,
			});
		}
	}

	/**
	 * 听课列表初始化
	 */
	var courseInit = function(params){
		$servers.ajaxCourse(params,ajaxCourseCallBack);
	}

	/**
	 * 选择体重与BMI记录、胎动记录、自我记录、孕妇学校听课记录
	 */
	var onClassifySelect = function(){
		var params = $(this).data("params");
		var index = $(this).index();
		$(this).addClass("active").siblings().removeClass("active");
		$(".selfRecord-con .con").eq(index).show().siblings().hide();
		if( index == 0 ){
			$servers.ajaxPrepregnancyBmi(params,ajaxPrepregnancyBmiCallBack);
		}else if( index == 1 ){
			$servers.ajaxFetalmovement(params,ajaxFetalmovementCallBack);
		}else if( index == 2 ){
			$.extend(params,{"busiTypeList":["A0101"]});
			$selfRecordCommServers.ajaxUserCpTasks(params,ajaxUserCpTasksCallBack);
		}else if( index == 3 ){
			$.extend(params,{"rows":10,"page":1});
			courseInit(params);
		}
	}

	/**
	 * 患者基本信息
	 */
	var infoInit = function(info){
		$ui.info({
			"id":"selfRecordInfo",
			"data": info
		});
	}

	pregnancySelfRecord.init = function(){
		var params = $bb.getPageParams();
		infoInit(params.info);
		// 选择体重与BMI记录、胎动记录、自我记录、孕妇学校听课记录
		$(".selfRecord-classify > div").unbind("click");
		$(".selfRecord-classify > div").data("params",params);
		$(".selfRecord-classify > div").click(onClassifySelect);
		$(".selfRecord-classify > div").eq(0).click();
	}

	return pregnancySelfRecord;
});